AddPurchaseOrderWindow = Ext.extend(Ext.Window, {
//	setPurGridHeight : function(height) {
//		this.purchase_item_grid.setHeight(height);
//	},
//	setPurPanelHeight : function(height) {
//		this.purchase_panel.setHeight(height);
//	},
//	setJoinPanelHeight : function(height) {
//		this.join_panel.setHeight(height);
//	},
	partnerAddModuleId : {'moduleId' : 'partner_add', 'params' : {'partner_index' : 0}},
	setBuyerCode : function(code) {
		this.buyerCode = code;
		this.setTitle(this.creator.ordersName + code);
	},
	initPanel : function(initParams) {
		this.createAllHidden();
		this.createAllCmb();
		
		this.address_combox = this.createAddressCombox();
		
		this.priceEditor = new Ext.form.NumberField({
			regex : /([1-9][0-9]*(\.\d+)?)|(0\.\d+)/,
			regexText : '您只能够输入数字'
		});
		
		if(this.creator.ordersNameEn == 'online_purchase_order') {
			this.priceEditor.disable();
		}
		
		this.purchase_item_colName = this.createPurchaseItemColName();
		this.purchase_item_record = this.createPurchaseItemRecord();
		this.purchase_item_store = this.createPurchaseItemStore();
		
		
		this.partner_combox = this.createPartnerCombox();
		
		this.purchase_item_grid = this.createPurchaseItemGrid();
		this.purchase_panel = this.createPurchasePanel();
		
		this.purchase_item_colName.setHidden(3, true);
		
//		this.join_panel = this.createJoinPanel();
		
		this.capturePurStoreEvent();
		
		this.add(this.purchase_panel);
		
		this.on('close', function() {
			this.removeLinkQuotaWindow();
		});
		
		this.on('activate', function() {
			if(!this.myloaded) {
				this.loadContent(initParams);
				this.myloaded = true;
			}
			if(this.curr_record) {
				if( !this.validatePur(this.curr_record)) {
					this.curr_record = null;
					return;
				}
				
				var rec = this.getPurRecord(this.curr_record);
				this.purchase_item_store.add.defer(500, this.purchase_item_store, [rec]);
				this.curr_record = null;
			}
			
		}.createDelegate(this));
		
//		this.on('show', function() {
//		}, this, {single : true});
		
	},
		
	createAllCmb : function() {

		// 区县下拉框
		this.dist_cmb = getDistrictCombox(RES_PATH, this.purchase_areaId_hidden);// dist_cmb.setWidth(80);
		this.dist_cmb.hiddenName = null;
		this.dist_cmb.name = 'district';
		// 城市下拉框
		this.city_cmb = getCityCombox(RES_PATH, this.dist_cmb,
				this.purchase_areaId_hidden);
		this.city_cmb.hiddenName = null;
		this.city_cmb.name = 'city';
		// 省份下拉框
		this.prov_cmb = getProvinceCombox(RES_PATH, this.city_cmb,
				this.dist_cmb, this.purchase_areaId_hidden);
		this.prov_cmb.hiddenName = null;
		this.prov_cmb.name = 'province';

		// 如果要显示效验信息则必需
		Ext.QuickTips.init();

		Ext.form.Field.prototype.msgTarget = 'side';

		// 支付方式下拉框
		this.payType_store = getDictionaryStore(RES_PATH, {
					group : 'payType'
				});
		this.payType_cmb = getDictionaryComboBox(this.payType_store, 'remote',
				'paymentType', '支付方式', null, 100);
				
//		this.payType_cmb1 = Uec3.App.dictionary.getCombo("payType", {showWhole:true, width:100, hiddenName : 'paymentType'});

		// 运输方式下拉框
		this.transportType_store = getDictionaryStore(RES_PATH, {
					group : 'transportType'
				});
		this.transportType_cmb = getDictionaryComboBox(
				this.transportType_store, 'remote', 'transportType', '运输方式',
				null, 100);

//		this.transportType_cmb1 = Uec3.App.dictionary.getCombo("transportType", {showWhole:true, width:100, hiddenName : 'transportType'});
				
		// 来源下拉框
		this.sourceType_store = getDictionaryStore(RES_PATH, {
					group : 'part_source'
				});
		this.source_cmb = getDictionaryComboBox(this.sourceType_store, 'local',
				'part_source', '来源', 'source', 90);
				
		this.sourceType_store.load();
				
	},
	createAllHidden : function() {
		this.purchase_seller_id_hidden = new Ext.form.TextField({
					hidden : true,
					name : 'sellerId'
				});

		this.purchase_areaId_hidden = new Ext.form.TextField({
					hidden : true,
					name : 'areaId'
				});

		this.purchase_province_hidden = new Ext.form.TextField({
					hidden : true,
					name : 'province'
				});

		this.purchase_city_hidden = new Ext.form.TextField({
					hidden : true,
					name : 'city'
				});

		this.purchase_district_hidden = new Ext.form.TextField({
					hidden : true,
					name : 'district'
				});

		this.purchase_street_hidden = new Ext.form.TextField({
					hidden : true,
					name : 'street'
				});

		this.purchase_receiverName_hidden = new Ext.form.TextField({
					hidden : true,
					name : 'receiverName'
				});

		this.purchase_postCode_hidden = new Ext.form.TextField({
					hidden : true,
					name : 'postCode'
				});

		this.purchase_phone_hidden = new Ext.form.TextField({
					hidden : true,
					name : 'phone'
				});

		this.purchase_mobile_hidden = new Ext.form.TextField({
					hidden : true,
					name : 'mobile'
				});

		this.purchase_id_hidden = new Ext.form.TextField({
					hidden : true,
					name : 'id'
				});

		this.purchase_status_hidden = new Ext.form.TextField({
					hidden : true,
					name : 'status'
				});
				
		this.purchase_wkflId_hidden = new Ext.form.TextField({
					hidden : true,
					name : 'workFlowId'
				});
				
		this.purchase_partnerPyCodeByBuyer_hidden = new Ext.form.TextField({
					hidden : true,
					name : 'partnerPyCodeByBuyer'
				});

	},
	validatePur : function(record) {
		var records = this.purchase_item_store.getRange();
		for (var i = 0; i < records.length; i++) {
			var rec = records[i];
			if (rec.data.code + '&' + rec.data.source + '' == record.data.code + '&' + record.data.source + '') {
				Ext.Msg.alert(PurChaseAlert.PROMOTE_ERROR, '对不起，该行记录您已经选择..');
				return false;
			}
		}
		return true;
	},
	removeOldPurButtons : function() {
		this.purchase_panel.remove(PURCHASE_BUTTON_FDSET_ID + this.versionCode, true);
	},
	getModifyData : function() {
		var obj = {};
		obj.all = [];
		obj.add = [];
		obj.update = [];
		this.purchase_item_store.each(function(rec) {
			if (rec.data.id) {
				obj.all.push(rec.data.id);
			} else {
				obj.add.push(rec.data);
			}
		});
		
		var update = this.purchase_item_store.getModifiedRecords();
		Ext.each(update, function(rec) {
			if (rec.data.id) {
				obj.update.push(rec.data);
			}
		});
		return obj;
	},
	createNewPurButtons : function(btns) {
		var btnFieldset = new Ext.Toolbar({
			border : false,
			id : PURCHASE_BUTTON_FDSET_ID + this.versionCode,
			layout : 'hbox',
			height : 30,
			layoutConfig : {
				align : 'top',
				pack : 'center'
			},
			defaults : {
				margins : '0 10',
				buttonAlign : 'center',
				width : 70,
				height : 25
			}
		});

		this.purchase_panel.add(btnFieldset);
		this.purchase_panel.doLayout(true);

		var ref = this;
		for (var i = 0; i < btns.length; i++) {
			var obj = btns[i];
			var btn = new this.buttonMudule({
//				id : obj.id + this.versionCode,
				text : obj.value,
				width : 120,
				height : 22,
				url : RES_PATH + obj.url,
				func : obj.func,
				alertMeses : obj.alertMeses,
				tooltip : obj.tips,
				tooltipType : 'qtip',
				handler : function(bn) {
					bn.main_win = ref;
					if(bn.func == 'purOnlineNextOne' || bn.func == 'purNotOnlineNextOne' || bn.func == 'purOnlineQuery' || bn.func == 'purNotOnlineQuery') {
						eval('bn.' + bn.func + '()');
					} else {
						beforeClickBtnAlert.createDelegate(bn, [eval('bn.' + bn.func)])();
					}
				}
			});

			btnFieldset.add(btn);
			btnFieldset.doLayout(true);
		}
	},
	delAllRecords : function() {
		this.purchase_item_store.removeAll();
	},
	createRecieves : function() {
		this.creator.app.getDesktop().launchWindowByParam('receive_edit', {'orderId' : this.purchase_id_hidden.getValue(), 'codeOfBuyer' : this.purchase_panel.getForm().findField('codeOfBuyer').getValue()});
	},
	createPurchaseItemGrid : function() {
		function buyParts() {
			if(this.partner_combox.getValue() == null || (this.partner_combox.getValue()+'').trim() == '') {
				Ext.Msg.alert(PurChaseAlert.PROMOTE_ERROR, '对不起,您还没有选择供货单位..');
				return;
			}
			var desktop = Uec3.App.getDesktop();
			desktop.launchWindowByParam("quotationSearchWindowCreator", {
				purRecord : this.purchase_item_record, purGrid : this.purchase_item_grid, 
				compId : this.purchase_seller_id_hidden.getValue(), compName : this.partner_combox.getRawValue()});
		}
						
		this.purchase_toolbar = new Ext.Toolbar({
		  items:[
			{text : '[订购零件]', iconCls : 'icon-edit', handler : buyParts.createDelegate(this)},
			{text : '[删除全部]', iconCls : 'icon-del', handler : this.delAllRecords.createDelegate(this)}
		   ]
		});
		
		
		
		return new Ext.grid.EditorGridPanel({
			style : {padding : '2px'},
//			height : 300,
			clicksToEdit : 1,
			viewConfig : {
//				forceFit : true
			},
			flex : 1,
			cm : this.purchase_item_colName,
			store : this.purchase_item_store,
			tbar : this.purchase_toolbar,
			listeners : {
				'validateedit' : function(e) {
					if((e.value + '').trim() == '') {
						return false;
					}
				}
			}
		});
	},
	createPurchaseItemRecord : function() {
		return Ext.data.Record.create([{
					name : 'id'
				}, {
					name : 'code'
				}, {
					name : 'sellerId'
				}, {
					name : 'name'
				}, {
					name : 'carModel'
				}, {
					name : 'source'
				}, {
					name : 'measurementUnit'
				}, {
					name : 'brand'
				}, {
					name : 'origin'
				}, {
					name : 'price'
				}, {
					name : 'discount'
				}, {
					name : 'retailPrice'
				}, {
					name : 'orderQuantity'
				}, {
					name : 'quantity'
				}, {
					name : 'level1Id'
				}, {
					name : 'partId'
				}, {
					name : 'level1Name'
				}, {
					name : 'level2Name'
				}, {
					name : 'partCategoryId'
				}, {
					name : 'durability'
				}, {
					name : 'inventoryIdBySeller'
				}, {
					name : 'exec'
				}]);

	},
	createPurchaseItemStore : function() {
		return new Ext.data.JsonStore({
			url : this.loadDetailsUrl,
			baseParams : {},
			mode : "local",
			totalProperty : "results",
			root : "purchaseDetails",
			idProperty : "id",
			pruneModifiedRecords : true,
			fields : this.purchase_item_record
		});

	},
	createLinkQuotaWindow : function(initParams) {
		if(initParams.linkWindow) {
			this.linkMainWin = initParams.linkWindow;//alert(initParams.companyId);
			this.linkMainWin[initParams.companyId] = this;
		}
	},
	removeLinkQuotaWindow : function() {
		if(this.linkMainWin && this.purchase_seller_id_hidden) {
			this.linkMainWin[this.purchase_seller_id_hidden.getValue()] = null;
			this.linkMainWin = null;
		}
	},
	createPartnerCombox : function() {
		/**
		 * 供应商查询store
		 */
		var ds_partner_store = new Ext.data.JsonStore({
			url : this.partnerUrl,
			root : 'root',
			totalProperty : 'totalProperty',
			remoteSort : true,
			baseParams:{start : 0, limit : 4, type : 1, 'qualify' : this.partnerQualify},
			fields : [{
						name : 'id',
						type : 'int'
					}, {
						name : 'pyCode',
						type : 'string'
					}, {
						name : 'name',
						type : 'string'
					}, {
						name : 'companyId',
						type : 'int'
					}]
		});
				
//		var purStoreNum = this.purchase_item_store ? this.purchase_item_store.getCount() : 0;
		
		/**
		 * 供货商combox
		 */
		return new Ext.form.LookupBox2({
			store : ds_partner_store,
			displayField : 'name',
			lookupField : 'pyCode',
			valueField : 'id',
			hiddenId : 'partnerIdByBuyer' + new Date().getTime(),
			hiddenName : 'partnerIdByBuyer',
			typeAhead : false,
//			mode : 'local',
			triggerAction : 'all',
			emptyText : '输入拼音或名字查询供货商..',
			selectOnFocus : true,
			queryParam : 'queryParam',
			queryDelay : 10,
			typeAheadDelay : 10,
			pageSize : 4,
//			lookupCallBackEvents:['lookupChange'],
			start : 0,
			anchor : '100%',
			width : 318,
			app : this.creator.app,
			triggerTags : [
				{'moduleId' : 'partner_query', 'params' : {'partnerType' : 1, 'partnerQualify' : this.partnerQualify, 'purchase_item_store' : this.purchase_item_store}, 'triggerClass' : 'x-form-search-trigger'},
				this.partnerAddModuleId 
			],
			cleanValueTrigger : {
				triggerHandler : function() {
					this.readyToClearValue();
				},
				triggerClass : 'x-form-clear-trigger'
			},
			onSelect : function(record, index) {
				if (this.fireEvent('beforeselect', this, record, index) !== false) {
//					this.setValue(record.data[this.valueField], record.data[this.lookupField]);
					this.collapse();
					this.fireEvent('select', this, record, index);
				}
			},
			listeners : {
				'select' : function(lookup, record, index) {
					if(this.creator.ordersNameEn == 'online_purchase_order') {
						this.validateParnter(record.data.id, this.selectPartnerCmb.createDelegate(this, [lookup, record, index]));
					} else {
						this.selectPartnerCmb(lookup, record, index);
					}
				}.createDelegate(this)
				,
				'lookupChange' : function(record, creator, oldValue, win) {
					if(this.creator.ordersNameEn == 'online_purchase_order') {
						this.validateParnter(record.data.id, this.lookupChangePartnerCmb.createDelegate(this, [record, creator, oldValue, win]));
					} else {
						this.lookupChangePartnerCmb(record, creator, oldValue, win);
					}
				}.createDelegate(this)
				,
				'lookupClear' : function() {
					this.clearPartnerCmb();
				}.createDelegate(this)
					
			}
		});
	},
	validateParnter : function(partnerId, func) {
		var b = true;
		Ext.Ajax.request({
			url : RES_PATH + '/partner/validateOtherPartner.do',
			params : {'id' : partnerId },
			success : function(data) {
				var bo = Ext.decode(data.responseText);
				if(bo && bo.result + '' == 'notExist') {
					b = false;
					checkCertify(null, this.purchase_item_grid, null, null, bo.compId, bo.compName, bo.compId, ',把您加为往来单位', true, function(){});
//					Ext.Msg.confirm("提示", '对方供应商已将您公司从客户列表中删除，故暂不能下订单!是否发送请求，通知对方公司将您公司重新加为客户?', function(btnId) {
//						if(btnId == 'no') {
//							return;
//						} else {
//						}
//					}.createDelegate(this));
				} else if(bo && bo.result + '' == 'ok') {
					func();
				} else {}
			}.createDelegate(this)
		});
		return b;
	},
	selectPartnerCmb : function(lookup, record, index) {
		if(lookup.getValue() && (lookup.getValue() + '').trim() != '' && lookup.getValue() != record.data.id ) {
			var count = this.purchase_item_store.getCount();
			if(count > 0) {
				Ext.Msg.confirm("提示", '更改采购单位将删除原有零件记录，您确定修改吗？', function(btnId) {
					if(btnId == 'no') {
						return;
					} else {
						lookup.setValue(record.data[lookup.valueField], record.data[lookup.lookupField]);
						this.purchase_item_store.removeAll();
						this.purchase_seller_id_hidden.setValue(record.data.companyId);
						this.purchase_partnerPyCodeByBuyer_hidden.setValue(record.data.pyCode);
					}
				}.createDelegate(this));
			} else {
				lookup.setValue(record.data[lookup.valueField], record.data[lookup.lookupField]);
				this.purchase_seller_id_hidden.setValue(record.data.companyId);
				this.purchase_partnerPyCodeByBuyer_hidden.setValue(record.data.pyCode);
			}
		} else if(!lookup.getValue()) {
			lookup.setValue(record.data[lookup.valueField], record.data[lookup.lookupField]);
			this.purchase_seller_id_hidden.setValue(record.data.companyId);
			this.purchase_partnerPyCodeByBuyer_hidden.setValue(record.data.pyCode);
		} else {}
	},
	clearPartnerCmb : function() {
		var count = this.purchase_item_store.getCount();
		if(count > 0) {
			Ext.Msg.confirm("提示", '更改供货单位将删除原有零件记录，您确定修改吗？', function(btnId) {
				if(btnId == 'no') {
					return;
				} else {
					this.removeLinkQuotaWindow();
					this.partner_combox.clearValue();
					this.purchase_item_store.removeAll();
					this.purchase_seller_id_hidden.setValue(null);
					this.purchase_partnerPyCodeByBuyer_hidden.setValue(null);
				}
			}.createDelegate(this));
		} else {
			this.removeLinkQuotaWindow();
			this.partner_combox.clearValue();
			this.purchase_seller_id_hidden.setValue(null);
			this.purchase_partnerPyCodeByBuyer_hidden.setValue(null);
		}
	},
	lookupChangePartnerCmb : function(record, creator, oldValue, win) {
		if(oldValue && (oldValue + '').trim() != '' && record.data.id != oldValue ) {
			var count = this.purchase_item_store.getCount();
			if(count > 0) {
   				Ext.Msg.confirm("提示", '更改供货单位将删除原有零件记录，您确定修改吗？', function(btnId) {
   					if(btnId == 'no') {
						return;
					} else {
						this.removeLinkQuotaWindow();
				   	    creator.setLookupValue(record.data, win);
				   	    this.purchase_item_store.removeAll();
				   	    this.purchase_seller_id_hidden.setValue(record.data.companyId);
				   	    this.purchase_partnerPyCodeByBuyer_hidden.setValue(record.data.pyCode);
					}
		       	}.createDelegate(this));
			} else {
				this.removeLinkQuotaWindow();
   				creator.setLookupValue(record.data, win);
   				this.purchase_seller_id_hidden.setValue(record.data.companyId);
   				this.purchase_partnerPyCodeByBuyer_hidden.setValue(record.data.pyCode);
			}
		} else {
			if(record.data.id != oldValue) {
				this.removeLinkQuotaWindow();
				creator.setLookupValue(record.data, win);
				this.purchase_seller_id_hidden.setValue(record.data.companyId);
				this.purchase_partnerPyCodeByBuyer_hidden.setValue(record.data.pyCode);
			}
		}
					
	},
	validateSellerIsNotNull : function() {
		var partnerIdByBuyer = this.partner_combox.getValue();
		if ( !partnerIdByBuyer || (partnerIdByBuyer + '').trim() == '') {
			Ext.Msg.alert(PurChaseAlert.PROMOTE_ERROR, '对不起,您还没有选择供货单位..');
			return false;
		}
		return true;
	},
	/*getPurRecord : function(_record) {
		var data = _record.data
		var purData = {};
		purData.orderQuantity = 1;

		purData.name = data.name;
		purData.code = data.code;
		purData.source = data.source;
		purData.measurementUnit = data.measurementUnit;
		purData.brand = data.brand;
		purData.origin = data.origin;
		purData.price = data.retailPrice;
		purData.level1Name = data.level1Name;
		purData.level2Name = data.level2Name;
		purData.level1Id = data.level1Id;
		purData.partId = data.partId;
		purData.partCategoryId = data.partCategoryId;
		purData.durability = data.durability;
		purData.inventoryIdBySeller = data.inventoryId;
		purData.carModel = data.carModel;
		var record = new this.purchase_item_record(purData);
		
//		this.purchase_seller_id_hidden.setValue(data.companyId);
		
		return record;
	},*/
	getPurRecord : function(_record) {
		var data = _record.data
		var purData = {};
//		purData.orderQuantity = 1;

		purData.name = data.name;
		purData.code = data.code;
		purData.source = data.source;
		purData.measurementUnit = data.measurementUnit;
		purData.brand = data.brand;
		purData.origin = data.origin;
		purData.retailPrice = data.retailPrice;
		purData.discount = data.discount;
		purData.price = Ext.util.Format.number((data.discount/100) * (data.retailPrice), '0.00');
		purData.level1Name = data.level1Name;
		purData.level2Name = data.level2Name;
		purData.level1Id = data.level1Id;
		purData.partId = data.partId;
		purData.partCategoryId = data.partCategoryId;
		purData.durability = data.durability;
		purData.inventoryIdBySeller = data.inventoryId;
		purData.carModel = data.carModel;
		purData.orderQuantity = 1;
		var record = new this.purchase_item_record(purData);
		
		return record;
	},
	createDelPurBtn : function(value, metadata, record, rowIndex, colIndex, store) {
		
			if(this.status && this.status != '-1' && this.status != '0') {
				return '已确认';
			}
			
//			if(Uec3.App.getDesktop().windows.getActive() == this) {
//				alert('yiyang');
//			}
			
			return '<a style="font-size:12px;" href=javascript:getRunningWindow(' + 
				this.creatorIndex + 
				',Uec3.AddAutoPurchaseOrderWindowCreator).removeRecordFromStore("' + 
				record.id + '"); >删除</a>';
						
	},
	createPurchaseItemColName : function() {

		this.editor = new Ext.form.NumberField({
			regex : /([1-9][0-9]*(\.\d+)?)|(0\.\d+)/,
			regexText : '您只能够输入数字'
		});
		
		
		// 定义grid表头
		return new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), 
			{
				header : '操作',
				id : 'exec',
				dataIndex : 'id',
				renderer : this.createDelPurBtn.createDelegate(this)
			}, {
				header : '订购数量',
				dataIndex : 'orderQuantity',
				editor : this.editor
			}, {
				header : '销售数量',
				dataIndex : 'quantity'
			}, {
				header : '订购价',
				dataIndex : 'price',
				editor : this.priceEditor
			}, {
				header : '零件编码',
				dataIndex : 'code'
			}, {
				header : '零件名称',
				dataIndex : 'name'
			}, {
				header : '车型',
				dataIndex : 'carModel'
			}, {
				header : '来源',
				dataIndex : 'source',
				renderer : Ext.util.Format.comboRenderer(this.source_cmb)
			}, {
				header : '单位',
				dataIndex : 'measurementUnit'
			}, {
				header : '品牌',
				dataIndex : 'brand',
				sortable : true
			}, {
				header : '产地',
				dataIndex : 'origin',
				sortable : true
			}
		]);

	},
	createAddressCombox : function() {

		var ds_address_store = new Ext.data.JsonStore({
			url : Uec3.App.Constants.homeUrl
					+ "/receiptAddress/edit_receiptAddress!query.do",
			id : "id",
			root : 'root',
			totalProperty : 'totalProperty',
			remoteSort : true,
			baseParams : {
				sort : 'id',
				dir : 'ASC'
			},
			fields : [{
						name : 'areaId',
						type : 'string'
					}, {
						name : 'province',
						type : 'string'
					}, {
						name : 'city',
						type : 'string'
					}, {
						name : 'district',
						type : 'string'
					}, {
						name : 'street',
						type : 'string'
					}, {
						name : 'postCode',
						type : 'string'
					}, {
						name : 'receiverName',
						type : 'string'
					}, {
						name : 'phone',
						type : 'string'
					}, {
						name : 'mobile',
						type : 'string'
					}, {
						name : 'address',
						type : 'string'
					}],
			listeners : {
				'beforeload' : function(store, options) {

				}
//				,
//				'load' : function(store, records, options) {
//					Ext.each(records, function(record, index, array) {
//								var address = record.data.province
//										+ record.data.city
//										+ record.data.district
//										+ record.data.street;
//								record.set('address', address);
//							});
//				}
			}
		});

		return new Ext.form.LookupBox2({
			store : ds_address_store,
			displayField : 'address',
			lookupField : 'areaId',
			valueField : 'areaId',
			typeAhead : false,
			mode : 'local',
			triggerAction : 'all',
			emptyText : '选择收货地址..',
			selectOnFocus : true,
			lookupCallBackEvents:['lookupChange'],
			width : 400,
			anchor : '100%',
			editable : false,
			app : this.creator.app,
            triggerTags : [{moduleId:'receiptAddressQuery',triggerClass:'x-form-search-trigger'},{moduleId:'receiptAddressAdd'}],	
			listeners : {
				'lookupChange' : this.fillPartnerAddressInfo.createDelegate(this)
			}
		});

	},
	fillPartnerAddressInfo : function(record) {
		var fieldNames = ['areaId', 'province', 'city', 'district', 'street',
				'postCode', 'receiverName', 'phone', 'mobile'];
		Ext.each(fieldNames, function(fieldName, index, array) {
			if(record.data) {
				this.purchase_panel.getForm().findField(fieldName).setValue(record.data[fieldName]);
			} else {
				this.purchase_panel.getForm().findField(fieldName).setValue(record[fieldName]);
			}
		}.createDelegate(this));
	},
	createPurchasePanel : function() {

		var pur_formReader = new Ext.data.JsonReader({
			root : ""
//			totalProperty : 'totalProperty'
			}, [{
					name : 'id'
				}, {
					name : 'codeOfBuyer'
				}, {
					name : 'sellerId'
				}, {
					name : 'areaId'
				}, {
					name : 'sellerName'
				}, {
					name : 'memoOfBuyer'
				}, {
					name : 'memoOfSeller'
				}, {
					name : 'street'
				}, {
					name : 'receiverName'
				}, {
					name : 'postCode'
				}, {
					name : 'phone'
				}, {
					name : 'mobile'
				}, {
					name : 'province'
				}, {
					name : 'city'
				}, {
					name : 'district'
				}, {
					name : 'paymentType'
				}, {
					name : 'transportType'
				}, {
					name : 'statusDes'
				}, {
					name : 'status'
				}, {
					name : 'creator'
				}, {
					name : 'gmtCreate'
				}, {
					name : 'submitter'
				}, {
					name : 'gmtSubmit'
				}, {
					name : 'amount'
				}, {
					name : 'pyCode'
				}, {
					name : 'partnerName'
				}, {
					name : 'partnerNameByBuyer'
				}, {
					name : 'partnerIdByBuyer'
				}, {
					name : 'partnerPyCodeByBuyer'
				}
			]);

		// 采购订单主表头信息1
			
			var purchase_form_title = new Ext.Container({

					
						style : 'padding:0px;fontSize:13px;',
						layout : 'uec3grid',
						// width : '95%',
						height : 32,
						layoutConfig : {
							hGrid:[33,33,14,5,14,1],
            				vGrid:[20,70,8,4]
						},
						items : [{
									xtype : 'label',
									text : this.creator.ordersName,
									style : {
										font : 'bold 18px arial, serif',
										textAlign : 'center'
									},
									grid : [2, 2, 2, 2]
									
								}, {
									xtype : 'label',
									text : '单据号:',
									style : {
										fontSize : '13px'
									},
									grid : [4, 2, 4, 2]
								}, {
									xtype : 'textfield',
									name : 'codeOfBuyer',
									style : PurchaseColor.READ_ONLY_COLOR
											,
									grid : [5, 2, 5, 2],
									readOnly : true
								},
								{
					              html : '<hr color="#34A4E0" />',
					              grid:[1,4,6,4]
					             }
								]
					});
					
					
		var purchase_form_item1 = new Ext.Container({

					layout : 'uec3grid',
					height : 75,
					layoutConfig : {
							hGrid:[8,17,8,17,8,17,8,17],
            				vGrid:[5,31,31,31,2],
            				usedGrid:[[1,1,8,1]]
					},
						
					items : [{
									xtype : 'label',
									text : '供货单位:',
									style : {
										marginLeft : '1px',
										width : '81px'
									}
								}, Ext.apply(this.partner_combox,{grid:[2,2,4,2]}), this.purchase_wkflId_hidden,
								this.purchase_id_hidden,
								this.purchase_seller_id_hidden,
								this.purchase_areaId_hidden,
								this.purchase_province_hidden,
								this.purchase_city_hidden,
								this.purchase_district_hidden,
								this.purchase_street_hidden,
								this.purchase_receiverName_hidden,
								this.purchase_postCode_hidden,
								this.purchase_phone_hidden,
								this.purchase_mobile_hidden,
								this.purchase_status_hidden, 
								this.purchase_partnerPyCodeByBuyer_hidden, 
								{
									xtype : 'label',
									text : '支付方式:  '
								}, this.payType_cmb, {
									xtype : 'label',
									text : '运输方式:  '
								}, this.transportType_cmb, {
									xtype : 'label',
									text : '收货地址:  '
								}, 
								Ext.apply(this.address_combox,{grid:[2,3,8,3]}),
								{
									xtype : 'label',
									text : '买方备注:'
								}, {
									xtype : 'textarea',
									name : 'memoOfBuyer',
									grid:[2,4,4,4]
								
								}, {
									xtype : 'label',
									text : '卖方备注:'
								}, {
									xtype : 'textarea',
									name : 'memoOfSeller',
									style : PurchaseColor.READ_ONLY_COLOR,
									readOnly : true,
									grid:[6,4,8,4]
								
								}
					]
		});

		var maindata_item= new Ext.Container({
           layout:'vbox',
           flex:1,					
                    layoutConfig: {
                       align : 'stretch',
                       pack  : 'start'
                    },
            items:[purchase_form_item1, this.purchase_item_grid]
	   });
				
		// 采购订单主表头信息2
		var purchase_form_item2 = {
			style : 'padding:0px; fontSize:13px; marginTop:3px',
			layout : 'uec3grid',
			height : 28,
			layoutConfig : {
				hGrid:[7,10,6,10,7,10,6,10,7,10,7,10],
				vGrid:[10,80,10],
				usedGrid:[1,1,12,1]
			},
			items : [{
						xtype : 'label',
						text : '单据状态:',
						style : {
							marginLeft : '1px',
							width : '70px'
						}
					}, {
						xtype : 'textfield',
						name : 'statusDes',
						style : PurchaseColor.READ_ONLY_COLOR
								+ "; width : 100px",
						readOnly : true
					}, {
						xtype : 'label',
						text : '创建人:',
						style : {
							marginLeft : '5px',
							width : '60px'
						}
					}, {
						xtype : 'textfield',
						name : 'creator',
						style : PurchaseColor.READ_ONLY_COLOR
								+ "; width : 80px",
						readOnly : true
					}, {
						xtype : 'label',
						text : '创建时间:',
						style : {
							marginLeft : '5px',
							width : '70px'
						}
					}, {
						xtype : 'textfield',
						name : 'gmtCreate',
						style : PurchaseColor.READ_ONLY_COLOR
								+ "; width : 90px",
						readOnly : true
					}, {
						xtype : 'label',
						text : '提交人:',
						style : {
							marginLeft : '5px',
							width : '60px'
						}
					}, {
						xtype : 'textfield',
						name : 'submitter',
						style : PurchaseColor.READ_ONLY_COLOR
								+ "; width : 80px",
						readOnly : true
					}, {
						xtype : 'label',
						text : '提交时间:',
						style : {
							marginLeft : '5px',
							width : '70px'
						}
					}, {
						xtype : 'textfield',
						name : 'gmtSubmit',
						style : PurchaseColor.READ_ONLY_COLOR
								+ "; width : 90px",
						readOnly : true
					}, {
						xtype : 'label',
						text : '总金额:',
						style : {
							marginLeft : '5px',
							width : '60px'
						}
					}, {
						xtype : 'textfield',
						// id : 'pur_amount',
						name : 'amount',
						style : PurchaseColor.READ_ONLY_COLOR
								+ "; width : 90px",
						readOnly : true
					}]
		};

		// 表单数据
		// 表单数据
		return new Ext.FormPanel({
//					height : this.salePanelHeight,
					reader : pur_formReader,
					flex:1,
					layout:"vbox",
                    layoutConfig: {
                       align : 'stretch',
                       pack  : 'start'
                    },
					items : [purchase_form_title, maindata_item,
							purchase_form_item2]
				});

	},
	
	loadContent : function(initParams) {
		
		var virtual_btn = new this.buttonMudule();
		virtual_btn.main_win = this;
		if (initParams && initParams.id) {
			virtual_btn.url = this.initShowUrl;
			virtual_btn.alertMeses = {
				'ordersOk' : PurChaseAlert.PROMOTE_OK + ':订单查询成功',
				'ordersNotCurrentUser' : PurChaseAlert.PROMOTE_OK
						+ ':订单查询成功，您不是当前处理人',
				'ordersSubmit' : PurChaseAlert.PROMOTE_OK + ':订单查询成功，该单据已被提交',
				'ordersNotExist' : PurChaseAlert.PROMOTE_ERROR
						+ ':订单查询失败,该单据不存在或已被删除',
				'ordersError' : PurChaseAlert.PROMOTE_ERROR + ':订单查询失败'
			};

			virtual_btn.sendDataAndGetMes({
//				'status' : initParams.init_status,
				'ordersId' : initParams.id
			}, false);

			initParams.id = undefined;
//			initParams.init_status = undefined;

		} else {
			virtual_btn.url = this.initCreateUrl;
			
			virtual_btn.sendDataAndGetMes(null, false);
			
			if(initParams.partnerId) {
				Ext.Ajax.request({
					url : RES_PATH + '/partner/getPartnerByIdAndOwnerId.do',
					params : {'id' : initParams.partnerId},
					success : function(data) {
						var bo = Ext.decode(data.responseText);
						if(bo.result == 'ok') {
							var partnerId = bo.id;
							var partnerName = bo.name;
							var pyCode = bo.pyCode;
							
							this.partner_combox.initializeValue({
								'name' : partnerName,
								'pyCode' : pyCode,
								'id' : partnerId
							});
							this.purchase_seller_id_hidden.setValue(bo.companyId);
							
						} else {
							
						}
					}.createDelegate(this)
				});
			}
			
		}

	},
	sumAmount : function() {
		var recs = this.purchase_item_store.getRange();

		var amount = 0.0;

		Ext.each(recs, function(rec) {
					amount += Ext.util.Format.number(rec.data.price)
							* Ext.util.Format.number(rec.data.orderQuantity);
				});
		amount = Ext.util.Format.number(amount, '0.00');
		this.purchase_panel.getForm().findField('amount').setValue(amount);
	},
	capturePurStoreEvent : function() {
		Ext.util.Observable.capture(this.purchase_item_grid.getView(),
			function(e) {
				if (e == 'rowsinserted' || e == 'rowupdated'
						|| e == 'rowremoved') {
					// 计数
					this.sumAmount.createDelegate(this)();
				}
			}.createDelegate(this));
			
		
	},
	
	removeRecordFromStore : function(id) {
		var rec = this.purchase_item_store.getById(id);
		this.purchase_item_store.remove(rec);
	},
	createJoinPanel : function() {
		return new Ext.Panel({
			frame : true,
			height : 600,
			autoScroll : true,
			items : [this.purchase_panel]
		});
	},
	
	getFormValues : function() {
		var form_values = this.purchase_panel.getForm().getValues();

		for (var j in form_values) {
			form_values[j] = castNoSelToNULL(form_values[j]);
		}

		form_values = Ext.util.JSON.encode(form_values);

		return form_values;
	},
	validatePurdata : function() {
		var value = this.partner_combox.getValue();
		if( !value ) {
			Ext.Msg.alert(PurChaseAlert.PROMOTE_ERROR, '对不起,您还没有选择供货单位..');
			return false;
		}
		if( isRecordNULL(this.purchase_item_store) ) {
			Ext.Msg.alert(PurChaseAlert.PROMOTE_ERROR, '对不起,您的行记录不能为空...');
			return false;
		}
		
		var payType = this.payType_cmb.getValue();
		if( payType == null || (payType + '').trim() == '' ) {
			Ext.Msg.alert(PurChaseAlert.PROMOTE_ERROR, '对不起,您还没有选择支付方式..');
			return false;
		}
		
		var transType = this.transportType_cmb.getValue();
		if( transType == null || (transType + '').trim() == '' ) {
			Ext.Msg.alert(PurChaseAlert.PROMOTE_ERROR, '对不起,您还没有选择运输方式..');
			return false;
		}
		
		var address = this.address_combox.getValue();
		if( !address) {
			Ext.Msg.alert(PurChaseAlert.PROMOTE_ERROR, '对不起,您还没有选择收货地址..');
			return false;
		}
		
		return true;
	}
});